import { defineStore } from 'pinia'
import { requserLogin } from '@/api/user'
export const userlogin = defineStore("user", {
  state() {
    return {
      userInfo: {
        // 及时你刷新了页面，或者重新启动了项目，仍然可以从本地存储中获取对应的值
        name: localStorage.getItem('name'),
        token: localStorage.getItem('token')
      }
    }
  },
  actions: {
    // 用户登录
    async userLogin(user) {
      const result = await requserLogin(user)
      if (result.code === 200) {
        console.log('请求发送成功了')
        this.userInfo = result.data
        // 在本地存储当中也在先存储一份
        localStorage.setItem('name', result.data.name)
        localStorage.setItem('token', result.data.token)


      } else {
        return Promise.reject(new Error(result.message))

      }


    },
   /*  clearuserinfo(state) {
      state.userInfo={name:'',token:''}
    } */
  }
})